/**
 *  获取文件夹相关信息
 *
 * @author 花狐
 * @QQ 137428857
 * @date 2015-08-23
 *
 *
 */
package aorey.io;

import java.io.File;
import java.util.List;

import java.util.ArrayList;

public class Folder {

    private File files = null;

    public Folder(String dir) {
        File f = new File(dir);
        if (f.isDirectory()) {
            this.files = f;
        }
    }

    /**
     * 获取文件夹里的文件
     *
     * @return
     */
    public List<FileInfo> getFiles() {
        if (this.files != null) {
            File[] fs = this.files.listFiles();
            List<FileInfo> fl = new ArrayList<FileInfo>();
            FileInfo fileInfo;
            int s = fs.length;
            for (int i = 0; i < s; i++) {
                if (fs[i].isFile()) {
                    fileInfo = new FileInfo();
                    fileInfo.fileName = this.getFileNameNoEx(fs[i].getName());
                    fileInfo.fileFullname = fs[i].getName();
                    fileInfo.filePath = fs[i].getAbsolutePath().replace("\\", "/");
                    fl.add(fileInfo);
                    fileInfo = null;
                }
            }
            return fl;
        }
        return null;
    }

    public String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
